import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useMemo } from "react";
import classes from "./accountLogin.module.less";
import { useLockFn, useMount, useReactive } from "ahooks";
import { Trans, useTranslation } from "react-i18next";
// import Drawer from "@mui/material/Drawer";
import Drawer from "../../compoents/drawer";
import classNames from "classnames";
import { useDispatch, useSelector } from "react-redux";
import { useLocalStorageSetState, useScriptLoad } from "../../hooks";
import { matchTheme } from "../../utils";
import toast from "../../compoents/toast";
import useServices from "../../hooks/useRequest";
import googleClient from "../../authServices/google";
import appleClient from "../../authServices/apple";
import twitterClient from "../../authServices/twitter";
import authConfig from "../../authServices/config";
import { googleAuth, appleAuth, twitterV1Auth } from "../../api/auth";
import { EVENT, KEYCONST } from "../../enums";
import kingsInstance from "../../instance";
import { LocalKeyMap } from "../../local";
import { isDomesticVersion } from "../../config";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var Privacy = function(props) {
    var t = useTranslation().t;
    var theme = useSelector(function(state) {
        return state.app.theme;
    });
    return /*#__PURE__*/ _jsx("div", {
        className: classes.privacy,
        style: props.style,
        children: /*#__PURE__*/ _jsx("div", {
            className: classes.policyText,
            children: /*#__PURE__*/ _jsx(Trans, {
                i18nKey: LocalKeyMap.agreePrivacy,
                components: [
                    /*#__PURE__*/ _jsx("span", {
                        onClick: function() {
                            window.open("https://www.nfkings.com/noa/TermsofUse.pdf");
                        },
                        className: classes.policy
                    }),
                    /*#__PURE__*/ _jsx("span", {
                        className: classes.policy,
                        onClick: function() {
                            return window.open("https://www.nfkings.com/noa/PrivacyPolicy.pdf");
                        }
                    })
                ]
            })
        })
    });
};
var loginType = [
    "google",
    "apple",
    "twitter",
    "mail",
    "phone"
].filter(function(type) {
    if (isDomesticVersion) {
        return ![
            "google",
            "twitter"
        ].includes(type);
    }
    return true;
});
function AccountLogin() {
    var _localStore_prevAccount, _localStore_prevAccount1, _localStore_prevAccount2, _localStore_prevAccount3, _localStore_prevAccount4, _localStore_prevAccount5, _localStore_prevAccount6;
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 2), localStore = _useLocalStorageSetState[0], setLocalStore = _useLocalStorageSetState[1];
    var state = useReactive({
        hasLogin: !!(localStore === null || localStore === void 0 ? void 0 : localStore.prevAccount),
        prevLoginType: localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount = localStore.prevAccount) === null || _localStore_prevAccount === void 0 ? void 0 : _localStore_prevAccount.type,
        checked: true,
        drawerOpen: false
    });
    var dispatch = useDispatch();
    var t = useTranslation().t;
    var catchLoad = useScriptLoad([
        googleClient,
        appleClient
    ]).catchLoad;
    var services = useServices({
        setUserInfo: dispatch.app.setUserInfo,
        googleAuth: googleAuth,
        appleAuth: appleAuth,
        twitterV1Auth: twitterV1Auth,
        getTwitterV1Code: twitterClient.getOauth1Code
    });
    var _useSelector = useSelector(function(state) {
        return {
            firstRender: state.app.firstRender,
            theme: state.app.theme
        };
    }), firstRender = _useSelector.firstRender, theme = _useSelector.theme;
    var loginList = useMemo(function() {
        if (state.hasLogin) {
            return loginType.filter(function(t) {
                var _localStore_prevAccount;
                return t !== (localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount = localStore.prevAccount) === null || _localStore_prevAccount === void 0 ? void 0 : _localStore_prevAccount.type);
            });
        }
        return loginType.filter(function(t) {
            return ![
                "twitter",
                "google",
                "apple"
            ].includes(t);
        });
    }, [
        state.hasLogin,
        localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount1 = localStore.prevAccount) === null || _localStore_prevAccount1 === void 0 ? void 0 : _localStore_prevAccount1.type
    ]);
    var threePartyLogin = [
        "twitter",
        "apple",
        "google"
    ];
    var lockFn = useLockFn(function() {
        var _ref = _async_to_generator(function(asyncFn) {
            var res;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            asyncFn()
                        ];
                    case 1:
                        res = _state.sent();
                        return [
                            2,
                            res
                        ];
                }
            });
        });
        return function(asyncFn) {
            return _ref.apply(this, arguments);
        };
    }());
    var handlerSuccess = function(type, accessData) {
        localStorage.setItem(KEYCONST.token, JSON.stringify({
            accessToken: accessData.token
        }));
        dispatch.app.setCoinInfos().then(function() {
            services.setUserInfo().then(function(userData) {
                var data = userData.data;
                setLocalStore({
                    prevAccount: {
                        account: data.email,
                        isEmail: true,
                        type: type,
                        nickname: data.nickname
                    }
                });
                kingsInstance.emit(EVENT.connect, _object_spread({}, data, accessData));
                if (accessData.ifFirst) {
                    dispatch.app.PUSH({
                        path: "/referralCode"
                    });
                    return;
                }
                dispatch.app.PUSH("/home");
            }).catch(function(err) {
                console.log("error=====>", err);
                toast.createToast({
                    message: err.msg,
                    type: "errToast"
                });
            });
        });
    };
    var loginMap = {
        facebook: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/facebook.svg"),
                dark: require("../../assets/icons/dark/facebook.svg")
            }),
            fillImg: "",
            handler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    return _ts_generator(this, function(_state) {
                        return [
                            2
                        ];
                    });
                }));
            }
        },
        instagram: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/instagram.svg"),
                dark: require("../../assets/icons/dark/ins.svg")
            }),
            fillImg: "",
            handler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    return _ts_generator(this, function(_state) {
                        return [
                            2
                        ];
                    });
                }));
            }
        },
        google: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/google.svg"),
                dark: require("../../assets/icons/dark/google.svg")
            }),
            fillImg: "",
            handler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    var res, authRes, err;
                    return _ts_generator(this, function(_state) {
                        switch(_state.label){
                            case 0:
                                _state.trys.push([
                                    0,
                                    4,
                                    ,
                                    5
                                ]);
                                catchLoad();
                                return [
                                    4,
                                    googleClient.init()
                                ];
                            case 1:
                                _state.sent();
                                return [
                                    4,
                                    googleClient.getCode()
                                ];
                            case 2:
                                res = _state.sent();
                                return [
                                    4,
                                    services.googleAuth({
                                        clientId: authConfig.GOOGLE_CLIENTID,
                                        clientSecret: authConfig.GOOGLE_SECRET,
                                        code: res.code,
                                        redirectUri: authConfig.REDIRECT_URI,
                                        onaAppId: authConfig.ONA_APPID,
                                        onaAppSecret: authConfig.ONA_APP_SECRET
                                    })
                                ];
                            case 3:
                                authRes = _state.sent();
                                if (authRes.code == 200) {
                                    handlerSuccess("google", authRes.data);
                                    return [
                                        2
                                    ];
                                }
                                throw authRes;
                            case 4:
                                err = _state.sent();
                                console.log("error=====>", err);
                                toast.createToast({
                                    message: err.msg || err.message,
                                    type: "errToast"
                                });
                                return [
                                    3,
                                    5
                                ];
                            case 5:
                                return [
                                    2
                                ];
                        }
                    });
                }));
            }
        },
        apple: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/apple.svg"),
                dark: require("../../assets/icons/dark/apple.svg")
            }),
            fillImg: "",
            handler: function() {
                return lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    var res, authRes, err;
                    return _ts_generator(this, function(_state) {
                        switch(_state.label){
                            case 0:
                                _state.trys.push([
                                    0,
                                    4,
                                    ,
                                    5
                                ]);
                                catchLoad();
                                return [
                                    4,
                                    appleClient.init()
                                ];
                            case 1:
                                _state.sent();
                                return [
                                    4,
                                    appleClient.getCode()
                                ];
                            case 2:
                                res = _state.sent();
                                return [
                                    4,
                                    services.appleAuth({
                                        onaAppId: authConfig.ONA_APPID,
                                        onaAppSecret: authConfig.ONA_APP_SECRET,
                                        identityToken: res.id_token
                                    })
                                ];
                            case 3:
                                authRes = _state.sent();
                                if (authRes.code == 200) {
                                    handlerSuccess("apple", authRes.data);
                                    return [
                                        2
                                    ];
                                }
                                throw authRes;
                            case 4:
                                err = _state.sent();
                                toast.createToast({
                                    message: err.msg || err.error,
                                    type: "errToast"
                                });
                                return [
                                    3,
                                    5
                                ];
                            case 5:
                                return [
                                    2
                                ];
                        }
                    });
                }));
            }
        },
        twitter: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/twitter.svg"),
                dark: require("../../assets/icons/dark/twitter.svg")
            }),
            fillImg: "",
            handler: function() {
                lockFn(/*#__PURE__*/ _async_to_generator(function() {
                    var res, authRes, err;
                    return _ts_generator(this, function(_state) {
                        switch(_state.label){
                            case 0:
                                _state.trys.push([
                                    0,
                                    3,
                                    ,
                                    4
                                ]);
                                return [
                                    4,
                                    services.getTwitterV1Code()
                                ];
                            case 1:
                                res = _state.sent();
                                return [
                                    4,
                                    services.twitterV1Auth({
                                        onaAppId: authConfig.ONA_APPID,
                                        onaAppSecret: authConfig.ONA_APP_SECRET,
                                        token: res.oauth_token,
                                        verifier: res.oauth_verifier
                                    })
                                ];
                            case 2:
                                authRes = _state.sent();
                                if (authRes.code === 200) {
                                    handlerSuccess("twitter", authRes.data);
                                    return [
                                        2
                                    ];
                                }
                                throw authRes;
                            case 3:
                                err = _state.sent();
                                toast.createToast({
                                    message: err.msg || err.message,
                                    type: "errToast"
                                });
                                return [
                                    3,
                                    4
                                ];
                            case 4:
                                return [
                                    2
                                ];
                        }
                    });
                }));
            }
        },
        phone: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/phone.svg"),
                dark: require("../../assets/icons/dark/phoneSolid.svg")
            }),
            fillImg: "",
            handler: function() {
                dispatch.app.PUSH({
                    path: "/otherLogin",
                    search: "phone"
                });
            }
        },
        mail: {
            img: matchTheme(theme, {
                light: require("../../assets/icons/light/mail.svg"),
                dark: require("../../assets/icons/dark/mailSolid.svg")
            }),
            fillImg: "",
            handler: function() {
                dispatch.app.PUSH({
                    path: "/otherLogin",
                    search: "mail"
                });
            }
        }
    };
    useMount(function() {
        if (firstRender) {
            setTimeout(function() {
                dispatch.app.RENDER();
            }, 1300);
        }
    });
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            /*#__PURE__*/ _jsxs("div", {
                className: classNames(classes.account_login, state.drawerOpen ? classes.page_blur : ""),
                children: [
                    state.hasLogin ? /*#__PURE__*/ _jsxs("div", {
                        className: classes.continued,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.logo,
                                children: /*#__PURE__*/ _jsx("img", {
                                    src: matchTheme(theme, {
                                        light: require("../../assets/icons/light/logo.svg"),
                                        dark: require("../../assets/icons/dark/logo.svg")
                                    })
                                })
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.c_title,
                                children: t(LocalKeyMap.continueWith)
                            }),
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.avatar,
                                onClick: loginMap[(localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount2 = localStore.prevAccount) === null || _localStore_prevAccount2 === void 0 ? void 0 : _localStore_prevAccount2.type) || ((localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount3 = localStore.prevAccount) === null || _localStore_prevAccount3 === void 0 ? void 0 : _localStore_prevAccount3.isEmail) ? "mail" : "phone" || "phone")].handler,
                                children: [
                                    /*#__PURE__*/ _jsx("img", {
                                        src: loginMap[(localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount4 = localStore.prevAccount) === null || _localStore_prevAccount4 === void 0 ? void 0 : _localStore_prevAccount4.type) || ((localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount5 = localStore.prevAccount) === null || _localStore_prevAccount5 === void 0 ? void 0 : _localStore_prevAccount5.isEmail) ? "mail" : "phone" || "phone")].img
                                    }),
                                    /*#__PURE__*/ _jsx("span", {
                                        className: classes.nick,
                                        children: localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount6 = localStore.prevAccount) === null || _localStore_prevAccount6 === void 0 ? void 0 : _localStore_prevAccount6.nickname
                                    })
                                ]
                            })
                        ]
                    }) : /*#__PURE__*/ _jsxs("div", {
                        className: classes.top_content,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.logo,
                                children: /*#__PURE__*/ _jsx("img", {
                                    src: matchTheme(theme, {
                                        light: require("../../assets/icons/light/logoWithText.svg"),
                                        dark: require("../../assets/icons/dark/logo.svg")
                                    })
                                })
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.title,
                                children: t(LocalKeyMap["accountLogin.title"])
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.sign_content,
                                children: threePartyLogin.map(function(item) {
                                    return /*#__PURE__*/ _jsx("div", {
                                        onClick: loginMap[item].handler,
                                        className: classes.recommend_login,
                                        children: /*#__PURE__*/ _jsx("img", {
                                            src: loginMap[item].img
                                        })
                                    }, item);
                                })
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        // style={{
                        //   display: !state.hasLogin ? 'none' : 'flex',
                        // }}
                        className: classes.split_line,
                        children: /*#__PURE__*/ _jsx("div", {
                            className: classes.text,
                            children: /*#__PURE__*/ _jsx("span", {
                                children: "or"
                            })
                        })
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        className: classNames(classes.other),
                        children: loginList.map(function(item) {
                            return /*#__PURE__*/ _jsx("div", {
                                onClick: loginMap[item].handler,
                                className: classes.other_item,
                                children: /*#__PURE__*/ _jsx("img", {
                                    src: loginMap[item].img
                                })
                            }, item);
                        })
                    }),
                    /*#__PURE__*/ _jsx(Privacy, {
                        checked: state.checked
                    })
                ]
            }),
            /*#__PURE__*/ _jsxs(Drawer, {
                className: classes.drawer,
                direction: "up",
                open: state.drawerOpen,
                onClose: function() {
                    state.drawerOpen = false;
                },
                children: [
                    /*#__PURE__*/ _jsx("div", {
                        className: classes.mask
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes["drawer_content"],
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.v
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.title,
                                children: t(LocalKeyMap["accountLogin.other"])
                            }),
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.ways,
                                children: [
                                    /*#__PURE__*/ _jsx("div", {
                                        className: classes["wary_item"],
                                        onClick: function() {
                                            state.drawerOpen = false;
                                            dispatch.app.PUSH({
                                                path: "/otherLogin",
                                                search: "phone"
                                            });
                                        },
                                        children: /*#__PURE__*/ _jsx("img", {
                                            src: matchTheme(theme, {
                                                light: require("../../assets/icons/light/phone.svg"),
                                                dark: require("../../assets/icons/dark/phone.svg")
                                            })
                                        })
                                    }),
                                    /*#__PURE__*/ _jsx("div", {
                                        className: classes["wary_item"],
                                        onClick: function() {
                                            state.drawerOpen = false;
                                            dispatch.app.PUSH({
                                                path: "/otherLogin",
                                                search: "mail"
                                            });
                                        },
                                        children: /*#__PURE__*/ _jsx("img", {
                                            src: matchTheme(theme, {
                                                light: require("../../assets/icons/light/mail.svg"),
                                                dark: require("../../assets/icons/dark/mail.svg")
                                            })
                                        })
                                    })
                                ]
                            }),
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.space,
                                children: /*#__PURE__*/ _jsx(Privacy, {
                                    checked: state.checked,
                                    style: {
                                        marginTop: 0
                                    }
                                })
                            })
                        ]
                    })
                ]
            })
        ]
    });
}
export default AccountLogin;

 //# sourceMappingURL=index.js.map